<?php

$active_pages  = count($db->fetchCol("SELECT p.id FROM site_pages p LEFT JOIN site_pages_revisions r ON p.revision_id=r.id WHERE template_id=?", $_POST['id']));
$pending_pages = count($db->fetchCol("SELECT p.id FROM site_pages_revisions r JOIN site_pages p ON r.page_id=p.id WHERE r.id=(SELECT MAX(id) FROM site_pages_revisions WHERE page_id=r.page_id) AND template_id=? AND p.revision_id!=r.id;", $_POST['id']));

if ($active_pages == 0 and $pending_pages == 0) {
	$db->delete('site_templates', $db->quoteInto("id=?", $_POST['id']) . ' AND ' . $db->quoteInto("site_id=?", $site['id']));
	echo json_encode(array('id' => $_POST['id']));
}